home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1997 #3 / Amiga Plus CD - 1997 - No. 03.iso / pd / demo-versionen / maxoncpp4-demo / demo / supercode / code.c < prev    next >
C/C++ Source or Header  |  1996-12-31  |  9KB  |  502 lines

  1. //-------------------------------------
  2. //
  3. // SuperCode (c) 1996 by T.Kühn 
  4. //
  5. // Programmiersprache:    ANSI-C
  6. // Projektstart:            12.7.95
  7. //
  8. // Modul:                Super-Code
  9. //
  10. //-------------------------------------
  11. // max_equal * num_colors >=    num_columns
  12. //-------------------------------------
  13.  
  14.  
  15. #include <clib/alib_protos.h>
  16. #include <pragma/graphics_lib.h>
  17. #include <pragma/intuition_lib.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20.  
  21.  
  22. #include <struct.h>
  23.  
  24.  
  25.  
  26.  
  27. struct Field player1;
  28.  
  29. //-------------------------------------
  30. ULONG random(ULONG n)
  31. {
  32.     ULONG r;
  33.  
  34.     r=(rand()-1)*(n+1)/RAND_MAX;
  35.  
  36.     return r;
  37. }
  38. //-------------------------------------
  39.  
  40.  
  41. //-------------------------------------
  42. struct GameEntry *code_getnexttry(struct GameEntry *old)
  43. {
  44.     struct GameEntry *entry=0;
  45.  
  46.     if (old)
  47.     {
  48.         entry=(struct GameEntry *)old->node.ln_Succ;
  49.         if (!entry->node.ln_Succ) entry=0;
  50.     }
  51.     return entry;
  52. }
  53. //-------------------------------------
  54. struct GameEntry *code_getprevtry(struct GameEntry *old)
  55. {
  56.     struct GameEntry *entry=0;
  57.  
  58.     if (old)
  59.     {
  60.         entry=(struct GameEntry *)old->node.ln_Pred;
  61.         if (entry)
  62.             if (entry->node.ln_Pred)
  63.                 if (entry->node.ln_Pred->ln_Pred)
  64.                 {
  65.                 }
  66.                 else
  67.                 {
  68.                     entry=0;
  69.                 }
  70.     }
  71.     return entry;
  72. }
  73. //-------------------------------------
  74. struct GameEntry *code_get1sttry(struct Field *player)
  75. {
  76.     struct GameEntry *entry=(struct GameEntry *)player->try.lh_Head;
  77.  
  78.     entry=code_getnexttry(entry);
  79.  
  80.     return entry;
  81. }
  82. //-------------------------------------
  83. struct GameEntry *code_getnumtry(struct Field *player,LONG n)
  84. {
  85.     struct GameEntry *entry=code_get1sttry(player);
  86.  
  87.     while (--n>0)
  88.     {
  89.         entry=code_getnexttry(entry);
  90.     }
  91.  
  92.     return entry;
  93. }
  94. //-------------------------------------
  95. struct GameEntry *code_getcombi(struct Field *player)
  96. {
  97.     struct GameEntry *entry=(struct GameEntry *)player->try.lh_Head;
  98.     return entry;
  99. }
  100. //-------------------------------------
  101. struct GameEntry *code_getlsttry(struct Field *player)
  102. {
  103.     struct GameEntry *entry=(struct GameEntry *)player->try.lh_TailPred;
  104.  
  105.     return entry;
  106. }
  107. //-------------------------------------
  108.  
  109.  
  110.  
  111. //-------------------------------------
  112. ULONG code_getpoints(struct Field *player)
  113. {
  114.     ULONG x1=player->columns;
  115.     ULONG x2=player->num_colors;
  116.     LONG y=0;
  117.     LONG f2,f1;
  118.  
  119.     f1=MIN(player->max_equal,x1);
  120.     f2=1;
  121.  
  122.     switch (player->val_order)
  123.     {
  124.         case ORDER_POS:
  125.             f1=1;
  126.             f2=1;
  127.             break;
  128.         case ORDER_DESC:
  129.             f2=3;
  130.             break;
  131.         case ORDER_RANDOM:
  132.             f2=4;
  133.             break;
  134.     }
  135.  
  136.     y= (x1*x1 * x2*x2 * f1 * f2 - x1 * x2 * player->max_equal * (player->lines-1)) * 10;
  137.  
  138.     if (y<0) y=0;
  139.  
  140.     return y;
  141. }
  142. //-------------------------------------
  143.  
  144.  
  145. //-------------------------------------
  146. void code_fieldclear(struct Field *player)
  147. {
  148.     if (player)
  149.     {
  150.         List_Free(&player->try);
  151.     }
  152. }
  153. //-------------------------------------
  154. void code_freefield(struct Field *player)
  155. {
  156.     if (player)
  157.     {
  158.         code_fieldclear(player);
  159.         player->columns=0;
  160.     }
  161. }
  162. //-------------------------------------
  163. struct GameEntry *code_newfield(struct Field *player)
  164. {
  165.     struct GameEntry *entry;
  166.     LONG size=sizeof(struct GameEntry)+2*player->columns;
  167.  
  168.     entry=(struct GameEntry*)Memory_Alloc(size);
  169.     entry->size=size;
  170.     entry->field=((UBYTE*)entry)+sizeof(struct GameEntry);
  171.     entry->value=((UBYTE*)entry)+sizeof(struct GameEntry)+player->columns;
  172.  
  173.     player->lines++;
  174.  
  175.     List_AddTail(&player->try,&entry->node);
  176.  
  177.     return entry;
  178. }
  179. //-------------------------------------
  180.  
  181.  
  182. //-------------------------------------
  183. void code_resetplayer(struct Field *player)
  184. {
  185.     player->columns=prg_prefs->game.num_columns;
  186.     player->num_colors=prg_prefs->game.num_colors;
  187.     player->max_equal=prg_prefs->game.max_equal;
  188.     player->val_order=prg_prefs->game.val_order;
  189.     player->lines=0;
  190.     player->started=FALSE;
  191.  
  192.     while (player->max_equal * player->num_colors < player->columns)
  193.     {
  194.         player->num_colors++;
  195.     }
  196. }
  197. //-------------------------------------
  198. void code_initplayer(struct Field *player)
  199. {
  200.     if (player)
  201.     {
  202.         List_Init(&player->try,NT_TRY,0);
  203.  
  204.         code_resetplayer(player);
  205.     }
  206. }
  207. //-------------------------------------
  208.  
  209.  
  210. //-------------------------------------
  211. void code_newcomb(struct Field *player)
  212. {
  213.     if (player)
  214.     {
  215.         code_fieldclear(player);
  216.         code_resetplayer(player);
  217.     }
  218.     if (player)
  219.     {
  220.         UBYTE *equals=Memory_Alloc(player->num_colors+2);
  221.         struct GameEntry *entry;
  222.         ULONG t,w=player->columns;
  223.         ULONG n=player->num_colors;
  224.         UBYTE *field;
  225.  
  226.         entry=code_newfield(player);
  227.  
  228.         field=entry->field;
  229.  
  230.         srand( time(0) * VBeamPos() );
  231.         for (t=0;t<w;t++)
  232.         {
  233.             field[t]=random(n-1)+1;
  234.  
  235.             while (equals[field[t]] >= player->max_equal)
  236.             {
  237.                 field[t]=random(n-1)+1;
  238.             }
  239.             equals[field[t]]++;
  240.         }
  241.         player->finish=FALSE;
  242.         player->giveup=FALSE;
  243.         player->lines=0;
  244.  
  245.         Memory_Free(&equals);
  246.  
  247.         code_newfield(player);
  248.         player->started=FALSE;
  249.     }
  250. }
  251. //-------------------------------------
  252.  
  253.  
  254. //-------------------------------------
  255. void code_set(struct Field *player,ULONG pos,ULONG value)
  256. {
  257.     if (player)
  258.     {
  259.         struct GameEntry *entry=code_getlsttry(player);
  260.         ULONG w=player->columns;
  261.         ULONG n=player->num_colors;
  262.         UBYTE *ent=entry->field;
  263.  
  264.         ent[pos]=value;
  265.  
  266.         if (!player->started)
  267.         {
  268.             ULONG dummy;
  269.             CurrentTime(&player->sec_start,&dummy);
  270.             player->started=TRUE;
  271.             player->sec_start--;
  272.         }
  273.     }
  274. }
  275. //-------------------------------------
  276. ULONG code_getfield(struct Field *player,struct GameEntry *entry,ULONG pos)
  277. {
  278.     ULONG back=0;
  279.  
  280.     if (player && entry)
  281.     {
  282.         UBYTE *ent=entry->field;
  283.         ULONG w=player->columns;
  284.  
  285.         if (pos<w) back=ent[pos];
  286.     }
  287.     return back;
  288. }
  289. //-------------------------------------
  290. ULONG code_getvalue(struct Field *player,struct GameEntry *entry,ULONG pos)
  291. {
  292.     ULONG back=0;
  293.  
  294.     if (player && entry)
  295.     {
  296.         UBYTE *ent=entry->value;
  297.         ULONG w=player->columns;
  298.  
  299.         if (pos<w) back=ent[pos];
  300.     }
  301.     return back;
  302. }
  303. //-------------------------------------
  304. void code_comparelast(struct Field *player)
  305. {
  306.     if (player)
  307.     {
  308.         struct GameEntry *combi=code_getcombi(player);
  309.         struct GameEntry *entry=code_getlsttry(player);
  310.         ULONG tot=0,set=0;
  311.         ULONG w=player->columns;
  312.         ULONG n=player->num_colors;
  313.         ULONG s,t;
  314.         UBYTE *val=entry->value;
  315.  
  316.         UBYTE *com=Memory_Alloc(2*w);
  317.         UBYTE *ent=com+w;
  318.  
  319.         memcpy(com,combi->field,w);
  320.         memcpy(ent,entry->field,w);
  321.  
  322.         for (t=0;t<w;t++)
  323.         {
  324.             if (ent[t]>0) set++;
  325.         }
  326.  
  327.         if (set==w)
  328.         {
  329.             for (t=0;t<w;t++)
  330.             {
  331.                 if (com[t]==ent[t])
  332.                 {
  333.                     tot++;
  334.                     val[t]=BLACK;            // schwarzes
  335.                     com[t]=0;
  336.                     ent[t]=0;
  337.                 }
  338.             }
  339.     
  340.             for (t=0;t<w;t++)
  341.             {
  342.                 if (ent[t]>0)
  343.                 {
  344.                     for (s=0;s<w;s++)
  345.                     {
  346.                         if (com[s]==ent[t])
  347.                         {
  348.                             val[t]=WHITE;    // weisses
  349.                             com[s]=0;
  350.                             ent[t]=0;
  351.                         }
  352.                     }
  353.                 }
  354.             }
  355.     
  356.             if (tot==w)
  357.             {
  358.                 ULONG dummy;
  359.                 player->finish=TRUE;
  360.                 CurrentTime(&player->sec_finish,&dummy);
  361.             }
  362.             else if (player->val_order==ORDER_DESC)
  363.             {
  364.                 BOOL chg=TRUE;
  365.  
  366.                 while(chg)
  367.                 {
  368.                     chg=FALSE;
  369.                     for(t=0;t<(w-1);t++)
  370.                     {
  371.                         if (val[t] < val[t+1])
  372.                         {
  373.                             chg=TRUE;
  374.                             s=val[t];
  375.                             val[t]=val[t+1];
  376.                             val[t+1]=s;
  377.                         }
  378.                     }
  379.                 }
  380.             }
  381.             else if (player->val_order==ORDER_RANDOM)
  382.             {
  383.                 ULONG p1;
  384.                 ULONG p2;
  385.                 for(t=0;t<3*w;t++)
  386.                 {
  387.                     p1=random(w-1);
  388.                     p2=random(w-1);
  389.                     if (p1!=p2)
  390.                     {
  391.                         s=val[p1];
  392.                         val[p1]=val[p2];
  393.                         val[p2]=s;
  394.                     }
  395.                 }
  396.             }
  397.  
  398.             if (!player->finish)
  399.             {
  400.                 code_newfield(&player1);
  401.             }
  402.         }
  403.         Memory_Free(&com);
  404.     }
  405. }
  406. //-------------------------------------
  407. void code_giveup(struct Field *player)
  408. {
  409.     struct GameEntry *combi=code_getcombi(player);
  410.     ULONG w=player->columns,t;
  411.  
  412.     if (!player->finish)
  413.     {
  414.         player->finish=TRUE;
  415.         player->giveup=TRUE;
  416.     
  417.         for (t=0;t<w;t++)
  418.         {
  419.             code_set(player,t,code_getfield(player,combi,t));
  420.         }
  421.     }
  422. }
  423. //-------------------------------------
  424.  
  425. /*
  426. //-------------------------------------
  427. void code_output(struct Field *player)
  428. {
  429.     struct GameEntry *entry=code_get1sttry(player);
  430.     ULONG w=player->columns;
  431.     ULONG n=0,t;
  432.  
  433.     printf("\14SUPER-CODE \n\n");
  434.  
  435.     while(entry)
  436.     {
  437.         printf("%2.2d : ",++n);
  438.         for (t=0;t<w;t++)
  439.         {
  440.             printf("%2.2d ",code_getfield(player,entry,t));
  441.         }
  442.         printf("  ");
  443.  
  444.         for (t=0;t<w;t++)
  445.         {
  446.             switch (code_getvalue(player,entry,t))
  447.             {
  448.                 case BLACK:    printf("S");break;
  449.                 case WHITE:    printf("w");break;
  450.                 default:        printf(".");break;
  451.             }
  452.         }
  453.         printf("\n\n");
  454.         entry=code_getnexttry(entry);
  455.     }
  456. }
  457. //-------------------------------------
  458. void code_test()
  459. {
  460.     ULONG t=1,s,v;
  461.     struct Field *player=&player1;
  462.  
  463.     code_output(&player1);
  464.  
  465.     while(!player->finish)
  466.     {
  467.         code_newfield(player);
  468.  
  469.         printf("\n%d. Versuch\n",t);
  470.  
  471.         for (s=0;s<player->columns;s++)
  472.         {
  473.             scanf("%d",&v);
  474.             if (v==0) return;
  475.             code_set(player,s,v);
  476.         }
  477.         code_comparelast(player);
  478.         code_output(player);
  479.         t++;
  480.     }
  481.  
  482. }
  483. //-------------------------------------
  484. */
  485.  
  486. //-------------------------------------
  487. void code_init()
  488. {
  489.     code_initplayer(&player1);
  490.  
  491.     code_newcomb(&player1);
  492.  
  493. //    code_test();
  494. }
  495. //-------------------------------------
  496. void code_free()
  497. {
  498.     code_freefield(&player1);
  499. }
  500. //-------------------------------------
  501.  
  502.